//
//  HQHHotListDetailCollectionViewCell.m
//  LastOne
//
//  Created by 小柠檬 on 15/11/5.
//  Copyright © 2015年 sc. All rights reserved.
//

#import "HQHHotListDetailCollectionViewCell.h"
#import "HTTPMethod.h"
#import "ChangeString.h"
#import "HQHListDetailTableViewCell.h"
#import <MJRefresh.h>
#import "FitHeader.h"
#import "NightHeader.h"
#import "GiFHUD.h"
@implementation HQHHotListDetailCollectionViewCell

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        self.dataSource = [NSMutableArray array];
        
#pragma mark tableView
        self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.contentView.frame.size.width, 554 * FitHeight)];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.tableFooterView = [[UITableView alloc]init];
        
        
        [self.contentView addSubview:_tableView];
        [GiFHUD setGifWithImageName:@"loading.gif"];
    }
    return self;
}

-(void)setType:(NSString *)type
{
    if (_type != type) {
        _type = type;
        
    }
    
#pragma mark 刷新加载
        _page = 1;
        [self getData];
    
//下拉刷新
    _tableView.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [_dataSource removeAllObjects];
        _page = 1;
        [self getData];
    }];
    
//上拉加载
    _tableView.footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        _page++;
        [self getData];
    }];
    
    
}


-(void)getData
{
    [GiFHUD show];
    
    self.HotIDdataSource = [NSMutableArray array];
    
    [HTTPMethod getDataByString:@"http://api2.pianke.me/read/columns_detail" BodyDic:[ChangeString changeStringToDictionary:[NSString stringWithFormat:@"auth=&client=1&deviceid=137794B8-2578-4119-B189-3E532E7C0F5B&limit=10&sort=hot&start=%ld&typeid=%@&version=3.0.6",_page * 10, _type]] WithDataBlock:^(id data) {
        
        NSDictionary *dataDic = [data objectForKey:@"data"];
        NSArray *listArray = [dataDic objectForKey:@"list"];
        for (NSDictionary *listDic in listArray) {
            HQHListDetailModel *listDetailModel = [[HQHListDetailModel alloc]init];
            [listDetailModel setValuesForKeysWithDictionary:listDic];
            [_dataSource addObject:listDetailModel];
            _HotIDdataSource = [listDic objectForKey:@"id"];
            
        }
        [_tableView reloadData];
        [GiFHUD dismiss];
        [_tableView.header endRefreshing];
        [_tableView.footer endRefreshing];

    }];
    

}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataSource.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 160 * FitHeight;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"cell";
    HQHListDetailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[HQHListDetailTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if (_dataSource.count != 0) {
        cell.listDetailModel = _dataSource[indexPath.row];
    }
    [cell NightWithType:UIViewColorTypeNormal];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *str = [_dataSource[indexPath.row]iD];
    [self.myHotDelegate passHotValue:_dataSource[indexPath.row] With:str];
}




/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
